package com.wu.gumomallcoupon.coupon.service.impl;

import com.wu.common.to.MemberPrice;
import com.wu.common.to.SkuReductionTo;
import com.wu.gumomallcoupon.coupon.entity.SmsMemberPriceEntity;
import com.wu.gumomallcoupon.coupon.entity.SmsSkuLadderEntity;
import com.wu.gumomallcoupon.coupon.service.SmsMemberPriceService;
import com.wu.gumomallcoupon.coupon.service.SmsSkuLadderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wu.common.utils.PageUtils;
import com.wu.common.utils.Query;

import com.wu.gumomallcoupon.coupon.dao.SmsSkuFullReductionDao;
import com.wu.gumomallcoupon.coupon.entity.SmsSkuFullReductionEntity;
import com.wu.gumomallcoupon.coupon.service.SmsSkuFullReductionService;


@Service("smsSkuFullReductionService")
public class SmsSkuFullReductionServiceImpl extends ServiceImpl<SmsSkuFullReductionDao, SmsSkuFullReductionEntity> implements SmsSkuFullReductionService {

    @Autowired
    private SmsSkuLadderService skuLadderService;
    @Autowired
    private SmsMemberPriceService memberPriceService;


    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<SmsSkuFullReductionEntity> page = this.page(
                new Query<SmsSkuFullReductionEntity>().getPage(params),
                new QueryWrapper<SmsSkuFullReductionEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public void saveSkuReductionTo(SkuReductionTo skuReductionTo) {
        if (skuReductionTo.getDiscount().compareTo(new BigDecimal(0)) > 0){
            SmsSkuLadderEntity skuLadder = new SmsSkuLadderEntity();
            skuLadder.setSkuId(skuReductionTo.getSkuId());
            skuLadder.setFullCount(skuReductionTo.getFullCount());
            skuLadder.setDiscount(skuReductionTo.getDiscount());
            skuLadder.setAddOther(skuReductionTo.getCountStatus());
            skuLadderService.save(skuLadder);
        }

        if (skuReductionTo.getReducePrice().compareTo(new BigDecimal(0)) > 0){
            SmsSkuFullReductionEntity skuFullReduction = new SmsSkuFullReductionEntity();
            skuFullReduction.setSkuId(skuReductionTo.getSkuId());
            skuFullReduction.setFullPrice(skuReductionTo.getFullPrice());
            skuFullReduction.setReducePrice(skuReductionTo.getReducePrice());
            skuFullReduction.setAddOther(skuReductionTo.getPriceStatus());
            this.save(skuFullReduction);
        }

        List<MemberPrice> memberPrice = skuReductionTo.getMemberPrice();
        List<SmsMemberPriceEntity> collect = memberPrice.stream().filter(x->x.getPrice().compareTo(new BigDecimal(0))>0)
                .map(item -> {
            SmsMemberPriceEntity memberPriceEntity = new SmsMemberPriceEntity();
            memberPriceEntity.setSkuId(skuReductionTo.getSkuId());
            memberPriceEntity.setMemberLevelId(item.getId());
            memberPriceEntity.setMemberPrice(item.getPrice());
            memberPriceEntity.setMemberLevelName(item.getName());
            return memberPriceEntity;
        }).collect(Collectors.toList());
        memberPriceService.saveBatch(collect);
    }

}